package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.api.AddClient;
import com.xindaoapp.happypet.api.Address;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.bean.BaseEntity;
import com.xindaoapp.happypet.bean.Fans;
import com.xindaoapp.happypet.bean.Fans.Fan;
import com.xindaoapp.happypet.utils.BitmapUtil;
import com.xindaoapp.happypet.utils.CommonParameter.UserState;
import com.xindaoapp.happypet.utils.IRequest;
import com.xindaoapp.happypet.utils.ProgressHUD;

/**
 * 我的粉丝
 * 
 * @author xindaoapp
 * 
 */
public class FansAdapter extends XinDaoBaseAdapter<Fan> {
    private final boolean isCanHandle;

    public FansAdapter(Context context, List<Fan> iniData, int pageSize, int res, int loadingRes,
            boolean isCanHandle) {
        super(context, iniData, pageSize, res, loadingRes);
        this.isCanHandle = isCanHandle;
    }

    @Override
    public void nextPage(int start, int size, final ILoadNextPageData<Fan> iLoadNextPageData) {
        super.nextPage(start, size, iLoadNextPageData);
        HappyPetApplication.get().getMoccaApi()
                .getFollowFans(UserState.getUserUid(), start, size, new IRequest<Fans>() {

                    @Override
                    public void request(Fans t) {
                        iLoadNextPageData.loadNextPageData(t == null ? null : t.array);
                    }
                });
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent, final Fan value) {
        ViewHolder holder = (ViewHolder) convertView.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.face = (ImageView) convertView.findViewById(R.id.face);
            holder.iv_guanzhu = (ImageView) convertView.findViewById(R.id.iv_guanzhu);
            holder.name = (TextView) convertView.findViewById(R.id.name);
            convertView.setTag(holder);
        }
        ImageLoader.getInstance().displayImage(value.userface, holder.face,
                BitmapUtil.getPersonLoading());
        holder.name.setText(value.username);
        if (isCanHandle) {
            holder.iv_guanzhu.setVisibility(View.VISIBLE);
        } else {
            holder.iv_guanzhu.setVisibility(View.GONE);
        }
        if (value.mutual.equals("0")) {
            holder.iv_guanzhu.setImageResource(R.drawable.other_guanzhu);
            holder.iv_guanzhu.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    RequestParams params = new RequestParams();
                    params.put("follow_uid", value.uid);
                    params.put("follow_username", value.username);
                    final ProgressHUD ProgressDialog =
                            ProgressHUD.show(mContext, "正在加载...", true, true, null);
                    AddClient.get(Address.GUANZHUOTHER, params, new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, String content) {
                            if (null != ProgressDialog && ProgressDialog.isShowing()) {
                                ProgressDialog.dismiss();
                            }
                            try {
                                BaseEntity parse = JSON.parseObject(content, BaseEntity.class);
                                if (parse.getResult().equals("0")) {
                                    value.mutual = "1";
                                    notifyDataSetChanged();
                                    Toast.makeText(mContext, "关注成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Toast.makeText(mContext, "关注失败", Toast.LENGTH_SHORT).show();
                                }
                            } catch (Exception e) {
                                notifyDataSetChanged();
                                Toast.makeText(mContext, "已关注", Toast.LENGTH_SHORT).show();
                            }
                            super.onSuccess(statusCode, content);
                        }

                        @Override
                        public void onFailure(Throwable error, String content) {
                            if (null != ProgressDialog && ProgressDialog.isShowing()) {
                                ProgressDialog.dismiss();
                            }
                            super.onFailure(error, content);
                        }
                    });
                }
            });
        } else if (value.mutual.equals("1")) {
            holder.iv_guanzhu.setImageResource(R.drawable.a_b);
            holder.iv_guanzhu.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    RequestParams params = new RequestParams();
                    params.put("follow_uid", value.uid);
                    final ProgressHUD ProgressDialog =
                            ProgressHUD.show(mContext, "正在加载...", true, true, null);
                    AddClient.get(Address.GUZNZHUDELETE, params, new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, String content) {
                            if (null != ProgressDialog && ProgressDialog.isShowing()) {
                                ProgressDialog.dismiss();
                            }
                            try {
                                value.mutual = "0";
                                notifyDataSetChanged();
                                // holder.iv_guanzhu.setImageResource(R.drawable.other_guanzhu);
                                Toast.makeText(mContext, "取消关注成功", Toast.LENGTH_SHORT).show();
                                super.onSuccess(statusCode, content);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(Throwable error, String content) {
                            if (null != ProgressDialog && ProgressDialog.isShowing()) {
                                ProgressDialog.dismiss();
                            }
                            Toast.makeText(mContext, "关注失败", Toast.LENGTH_SHORT).show();
                            super.onFailure(error, content);
                        }
                    });

                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        ImageView face;
        ImageView iv_guanzhu;
        TextView name;
    }
}
